// pagesCollect/pages/collect/edit/homestay/homestay.js
import { getHostelMsg, updateHostelMsg } from "../../../../../api/collect"
import { list } from "../../../../../api/homePage";

const app = getApp()

const rules = [{ key: 'name', msg: '请选输入民宿名称' },
  { key: 'intro', msg: '请选输入民宿简介' },
{
  type: 'list', key: 'hostelRooms', msg: '请添加民宿房间',
  rules: [{ key: 'roomType', msg: '请选择房间类型' },
  // { key: 'intro', msg: '请输入房间简介' },
  // { key: 'imageUrl', msg: '请上传房间图片' }
]
},
{ key: 'address', msg: '请选择民宿地址' },
{ key: 'longitude', msg: '请选择民宿定位' },
{ key: 'imageUrl', msg: '请上传民宿图片' },
{ key: 'parkLongitude', msg: '请选择停车场定位' }]

Page({
  data: {
    navData: {
      title: '民宿数据完善',
      level: 10
    },
    id: '',
    form: {
      hostelRooms: []
    },
    navHeight: 32,
    navTop: 48,
    position: {},
    parkPosition: {},
    imageStr: '',
    indexObj: {
      roomType: []
    },
    dict: {
      room_type: [],
    }
  },
  onLoad(e) {
    this.getDict('room_type');
    this.setData({
      navHeight: app.globalData.navHeight, //导航栏高度
      navTop: app.globalData.navTop, //导航栏距顶部距离
    });
    this.data.form.villageId = e.villageId;
    if(e.id){
      this.data.id = e.id;
      this.getVillage()
    }
  },
  getDict(key) {
    app.getDict(key).then(res => {
      this.setData({
        ['dict.' + key]: res.data
      })
    })
  },
  getVillage() {
    getHostelMsg({ id: this.data.id }).then(res => {
      if (!res.data.data.hostelRooms) {
        res.data.data.hostelRooms = [];
      }
      this.setData({
        ['navData.title']: res.data.data.name + '数据完善',
        form: res.data.data,
        position: {
          longitude: res.data.data.longitude,
          latitude: res.data.data.latitude
        },
        parkPosition: {
          longitude: res.data.data.parkLongitude,
          latitude: res.data.data.parkLatitude
        },
        imageStr: res.data.data.imageUrl
      })
      this.data.form.hostelRooms.forEach(item => {
        this.data.indexObj.roomType.push(0)
      })
      this.setData({
        ['indexObj.roomType']: this.data.indexObj.roomType
      })
    })
  },
  submit() {
    let sendData = JSON.parse(JSON.stringify(this.data.form));
    let errMsg = null;
    for (let i = 0; i < rules.length; i++) {
      let key = rules[i].key;
      if (rules[i].type == 'list') {
        if (!sendData[key] || sendData[key].length == 0) {
          errMsg = rules[i].msg;
        } else {
          let list = sendData[key],
            listRules = rules[i].rules;
          for (let j = 0; j < list.length; j++) {
            let data = list[j]
            for (let k = 0; k < listRules.length; k++) {
              let listkey = listRules[k].key;
              if (!data[listkey]) {
                errMsg = listRules[k].msg;
                break;
              }
            }
            if (errMsg) break;
          }
          if (errMsg) break;
        }
      } else if (!sendData[key]) {
        errMsg = rules[i].msg;
      }
      if (errMsg) break;
    }
    if (errMsg) {
      wx.showToast({
        icon: 'none',
        title: errMsg,
      })
      return false;
    }
    updateHostelMsg(sendData).then(res => {
      let pages = getCurrentPages();
      let prefrctPage = pages[pages.length - 2];
      prefrctPage.refresh()
      let listPage = pages[pages.length - 3];
      listPage.refresh()
      wx.showModal({
        content: res.data.msg,
        showCancel: false,
        complete: (res) => {
          if (res.confirm) {
            wx.navigateBack()
          }
        }
      })
    })
  },
  hostelRoomsDel(e) {
    let index = e.currentTarget.dataset.index,
      hostelRooms = this.data.form.hostelRooms;
    hostelRooms.splice(index, 1)
    this.data.indexObj.roomType.splice(index, 1)
    this.setData({
      ['indexObj.roomType']: this.data.indexObj.roomType,
      ['form.hostelRooms']: hostelRooms
    })

  },
  hostelRoomsAdd() {
    let hostelRooms = this.data.form.hostelRooms || [];
    hostelRooms.push({});
    this.data.indexObj.roomType.push(0)
    this.setData({
      ['indexObj.roomType']: this.data.indexObj.roomType,
      ['form.hostelRooms']: hostelRooms
    })
  },
  toEdit() {
    app.globalData.introduceStr = this.data.form.intro;
    wx.navigateTo({
      url: '/pagesCollect/pages/collect/edit/editor/editor?key=intro',
    })
  },
  roomTypeChg(e) {
    let index = e.currentTarget.dataset.index,
      value = e.detail.value;
    this.setData({
      ['indexObj.roomType[' + index + ']']: value,
      ['form.hostelRooms[' + index + '].roomType']: this.data.dict.room_type[value].dictValue
    })
  },
  intChg(e) {
    let key = e.currentTarget.dataset.key,
      value = e.detail.value;
    this.setData({
      ['form.' + key]: value
    })
  },
  dwChg(e) {
    let {
      longitude,
      latitude
    } = e.detail
    this.setData({
      ['form.longitude']: longitude,
      ['form.latitude']: latitude
    })
  },
  addressChg(e){
    this.setData({
      ['form.address']: e.detail,
    })
  },
  tccChg(e) {
    let {
      longitude,
      latitude
    } = e.detail
    this.setData({
      ['form.parkLongitude']: longitude,
      ['form.parkLatitude']: latitude
    })
  },
  imageChg(e) {
    let key = e.currentTarget.dataset.key,
      scalekey = e.currentTarget.dataset.scale;
    let imgStr = e.detail;
    let imgArr = imgStr.split(',')
    let imgScaleArr = imgArr.map(img => {
      let res = img.split('.');
      return res[0] + '-scale.' + res[1]
    })
    this.setData({
      ['form.' + key]: imgStr,
      ['form.' + scalekey]: imgScaleArr.join(','),
    })
  },
})